﻿using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SystemModule;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Huirui.Cavan.Presentation.Controller.SystemModule
{
    public class ReceiptCodeRuleController : EntityController<ReceiptCodeRule, ReceiptCodeRuleSearch, IReceiptCodeRuleViewModel, IReceiptCodeRuleAppService>, IReceiptCodeRuleController
    {
        public override string ControllerName
        {
            get { return ControllerConst.RECEIPTCODERULE; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public ReceiptCodeRuleController()
        {
            this.Model = ModelFactory.CreateModel<IReceiptCodeRuleViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "receiptcoderule_query")]
        public ActionResult AddReceiptCodeRule(int? id)
        {
            this.Model.IsDefaultOptions = new List<SelectListItem>();
            SelectListItem sli = new SelectListItem() { Selected = true, Text = "是", Value = "1" };
            this.Model.IsDefaultOptions.Add(sli);
            //sli = new SelectListItem() { Text = "否", Value = "0" };
            //this.Model.IsDefaultOptions.Add(sli);
            this.Model.Item = new ReceiptCodeRule();
            this.Model.Item.SerialNumberStart = 1;
            return View("~/Views/System/v1/addReceiptCodeRule.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "receiptcoderule_edit")]
        public JsonResult AddReceiptCodeRule(ReceiptCodeRuleViewModel model)
        {
            BoolResult jsonResult = null;
            model.Item.IsDefault = model.IsDefault == "1" ? true : false;
            model.Item.Pattern = (SystemIdentifier.CodePattern)model.Pattern;
            BoolGenericResult<ReceiptCodeRule> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.LIST)]
        [ActionPermissionAttribute(Name = "receiptcoderule_query")]
        public JsonResult GetReceiptCodeRules(int id)
        {
            ReceiptCodeRuleJsonViewModel json = new ReceiptCodeRuleJsonViewModel();
            json.rows = new List<ReceiptCodeRuleJson>();

            IList<ReceiptCodeRule> receiptCodeRules = this.AppService.GetBySearch(SearchFactory.CreateReceiptCodeRuleSearch(id)).ToList();
            foreach (ReceiptCodeRule rcr in receiptCodeRules)
            {
                ReceiptCodeRuleJson rcrj = new ReceiptCodeRuleJson();
                rcrj.Id = rcr.Id;
                rcrj.Name = rcr.Name;
                rcrj.Type = rcr.Type.GetHashCode();
                rcrj.Prefix = rcr.Prefix;
                rcrj.SerialNumberNumber = rcr.SerialNumberNumber;
                rcrj.SerialNumberStart = rcr.SerialNumberStart;
                rcrj.IsDefault = rcr.IsDefault ? "是" : "否";
                rcrj.Example = rcr.Example;
                rcrj.Pattern = rcr.Pattern.GetHashCode();
                rcrj.StrPattern = Ext.GetEnumDescription(typeof(SystemIdentifier.CodePattern), rcr.Pattern.GetHashCode().ToString());
                rcrj.YearNumber = rcr.YearNumber;
                json.rows.Add(rcrj);
            }
            json.total = receiptCodeRules.Count;

            return Json(json, "application/json");
        }

        [ActionName("example")]
        [ActionPermissionAttribute(IsUsedDenied=false)]
        public JsonResult GetReceiptCodeRuleExample(string pf, int snn, int sn, int p, int yn)
        {
            BoolResult result = new BoolResult();
            StringBuilder idSB = new StringBuilder();
            idSB.Append(pf);
            if ((SystemIdentifier.CodePattern)p != SystemIdentifier.CodePattern.SerialNumber)
            {
                if (yn == 2)
                {
                    idSB.Append(DateTime.Now.Year.ToString().Substring(2, 2));
                }
                else
                {
                    idSB.Append(DateTime.Now.Year.ToString());
                }
                if ((SystemIdentifier.CodePattern)p != SystemIdentifier.CodePattern.Year)
                {
                    idSB.Append(DateTime.Now.Month.ToString().PadLeft(2, '0'));
                }
                if ((SystemIdentifier.CodePattern)p == SystemIdentifier.CodePattern.Day)
                {
                    idSB.Append(DateTime.Now.Day.ToString().PadLeft(2, '0'));
                }
            }

            idSB.Append(sn.ToString().PadLeft(snn, '0'));

            result.Messages.Add(idSB.ToString());

            return Json(result);
        }

        [ActionName(ActionConst.DELETE)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "receiptcoderule_edit")]
        public JsonResult DeleteReceiptCodeRule(string selected)
        {
            BoolGenericResult<ReceiptCodeRule> result = null;
            List<ReceiptCodeRuleJson> receiptCodeRules = JsonConvert.DeserializeObject<List<ReceiptCodeRuleJson>>(selected);

            foreach (ReceiptCodeRuleJson json in receiptCodeRules)
            {
                ReceiptCodeRule br = this.AppService.GetById(json.Id);
                result = this.AppService.Delete(br);
            }

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }
    }
}
